using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Snake
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SoundEffect EatSound;
        object2D topBackgrund;
        object2D botBackgrund;
        object2D leftBackgrund;
        object2D rigthBackgrund;
        object2D midbackgrund;

        bool HasClicked = false;
        bool GameIsOn = false;
        bool HasIncSpeed = false;
        int index = 0;
            

        Player playernow;
        GUI GUinow;
        FoodObject FoodItem;
        Backgrund BackgrundNow;
        Menu MenuNow;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 400;
            graphics.PreferredBackBufferWidth = 600;
            this.IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            playernow = new Player(this.Content.Load<Texture2D>("SnakeBody"), new Vector2(300, 200) ,this.Content.Load<Texture2D>("SnakeBodyLeftRigth"));
            GUinow = new GUI(this.Content.Load<SpriteFont>("Time"),new Vector2(270,10));
            FoodItem = new FoodObject(this.Content.Load<Texture2D>("Food"));
            topBackgrund = new object2D(this.Content.Load<Texture2D>("TopWall"),new Vector2(50,0));
            botBackgrund = new object2D(this.Content.Load<Texture2D>("BotWall"), new Vector2(50, 350));
            leftBackgrund = new object2D(this.Content.Load<Texture2D>("LeftWall"), new Vector2(0, 0));
            rigthBackgrund = new object2D(this.Content.Load<Texture2D>("RigthWall"), new Vector2(550, 0));
            midbackgrund = new object2D(this.Content.Load<Texture2D>("Grass"), new Vector2(50, 50));
            BackgrundNow = new Backgrund(topBackgrund,botBackgrund,leftBackgrund,rigthBackgrund,midbackgrund);
            MenuNow = new Menu(this.Content.Load<Texture2D>("menuExit"), this.Content.Load<Texture2D>("menuStart"), this.Content.Load<Texture2D>("menuBackground"));
            EatSound = this.Content.Load<SoundEffect>("nom3");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            if (HasClicked == false)
            {
                if (MenuNow.GameExit())
                    this.Exit();
                bool Result = MenuNow.GameStart();
                GameIsOn = Result;
                HasClicked = Result;
            }
            if(GameIsOn)
            {
                if (GUinow.Time == 15 || GUinow.Time == 30 || GUinow.Time == 45 || GUinow.Time == 59)
                {
                    if (HasIncSpeed == false)
                    {
                        playernow.MovementSpeed = 0.30f;
                        HasIncSpeed = true;
                    }

                }
                else
                    HasIncSpeed = false;

                if (playernow.MovementSpeed >= 10)
                    index = 1;
                else
                {
                    index = 10;
                }
                for (int i = index; i < playernow.SnakeBodyArrayLength; i++)
                {
                    if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, playernow.GetSnakBodyPart(i).texture, playernow.snakepos, playernow.GetSnakBodyPart(i).pos))
                    {
                        ResetGame();
                    }
                }
                if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, topBackgrund.texture, playernow.snakepos, topBackgrund.pos))
                {
                    ResetGame();
                }
                if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, botBackgrund.texture, playernow.snakepos, botBackgrund.pos))
                {
                    ResetGame();
                }
                if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, leftBackgrund.texture, playernow.snakepos, leftBackgrund.pos))
                {
                    ResetGame();
                }
                if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, rigthBackgrund.texture, playernow.snakepos, rigthBackgrund.pos))
                {
                    ResetGame();
                }
                if (XNA2DCollisionDetection.CollisionDetection2D.PerPixel(playernow.snakebody, FoodItem.foodTexture, playernow.snakepos, FoodItem.foodPos))
                {
                    FoodItem.NewPos();
                    EatSound.Play(1F, 1F, 0F);
                    GUinow.points = 10;
                    playernow.AddBody();
                }
                playernow.Movement();
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Gray);

            // TODO: Add your drawing code here
            if (GameIsOn)
            {
                BackgrundNow.Draw(spriteBatch);
                playernow.Draw(spriteBatch);
                FoodItem.Draw(spriteBatch);
                GUinow.Draw(spriteBatch);
            }
            else
            {
                BackgrundNow.DrawWhitoutMid(spriteBatch);
                MenuNow.Draw(spriteBatch);
            }

            base.Draw(gameTime);
        }
        private void ResetGame()
        {
            playernow.reset();
            GUinow.reset();
            FoodItem.NewPos();
            GameIsOn = false;
            HasClicked = false;
        }
    }
}
